﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Razre.Base;
using Microsoft.Practices.Composite.Presentation.Commands;
using NavigationMVVMDemo.Models;
using NavigationMVVMDemo.Events;
using Razre;

namespace NavigationMVVMDemo.ViewModels
{
    public class PersonViewModel : BaseViewModel
    {
        public PersonViewModel()
        {
            AddPhoneNumberCommand = new DelegateCommand<object>(AddPhoneNumber);
        }

        private PersonModel _person = null;
        public PersonModel Person
        {
            get
            {
                return _person;
            }
            set
            {
                _person = value;
                OnPropertyChanged("Person");
            }
        }

        public DelegateCommand<object> AddPhoneNumberCommand { get; set; }

        private void AddPhoneNumber(object parameter)
        {
            PhoneNumberModel phoneNumber = new PhoneNumberModel();
            PhoneNumberViewModel viewmodel = ViewModelFactory.CreateViewModel<PhoneNumberViewModel>();
            viewmodel.PhoneNumber = phoneNumber;
            viewmodel.OnPhoneNumberSaved += new EventHandler<PhoneNumberFinishedEventArgs>(viewmodel_OnPhoneNumberSaved);

            NavigationManager.Navigate(viewmodel.View);
        }

        void viewmodel_OnPhoneNumberSaved(object sender, PhoneNumberFinishedEventArgs e)
        {
            if (e.IsSaved)
                Person.PhoneNumbers.Add(e.PhoneNumber);
        }


    }
}
